Fortran |
您所在的位置:网站首页 › character length argument mismatch › Fortran |
WIKI教程
学习Fortran
Fortran - 角色( Characters)
Fortran - 角色( Characters)
Fortran语言可以将字符视为单个字符或连续字符串。 字符可以是从基本字符集中取出的任何符号,即字母,十进制数字,下划线和21个特殊字符。 字符常量是固定值字符串。 内在数据类型character存储字符和字符串。 字符串的长度可以由len说明符指定。 如果未指定长度,则为1.您可以按位置引用字符串中的单个字符; 最左边的字符位于第1位。 品格宣言声明字符类型数据与其他变量相同 - type-specifier :: variable_name例如, character :: reply, sex你可以分配一个值,如, reply = ‘N’ sex = ‘F’以下示例演示了字符数据类型的声明和使用 - program hello implicit none character(len = 15) :: surname, firstname character(len = 6) :: title character(len = 25)::greetings title = 'Mr. ' firstname = 'Rowan ' surname = 'Atkinson' greetings = 'A big hello from Mr. Bean' print *, 'Here is ', title, firstname, surname print *, greetings end program hello编译并执行上述程序时,会产生以下结果 - Here is Mr. Rowan Atkinson A big hello from Mr. Bean 字符串联连接运算符//连接字符。 以下示例演示了这一点 - program hello implicit none character(len = 15) :: surname, firstname character(len = 6) :: title character(len = 40):: name character(len = 25)::greetings title = 'Mr. ' firstname = 'Rowan ' surname = 'Atkinson' name = title//firstname//surname greetings = 'A big hello from Mr. Bean' print *, 'Here is ', name print *, greetings end program hello编译并执行上述程序时,会产生以下结果 - Here is Mr.Rowan Atkinson A big hello from Mr.Bean Some 字符函数下表显示了一些常用的字符函数以及描述 - Sr.No 功能说明 1len(string) 它返回字符串的长度 2index(string,sustring) 它找到另一个字符串中子字符串的位置,如果找不到则返回0。 3achar(int) 它将整数转换为字符 4iachar(c) 它将字符转换为整数 5trim(string) 它返回删除了尾随空格的字符串。 6scan(string, chars) 对于“chars”中包含的任何字符的第一次出现,它从左到右搜索“string”(除非back = .true。)。 它返回一个给出该字符位置的整数,如果没有找到“chars”中的字符,则返回零。 7verify(string, chars) 它从左到右扫描“字符串”(除非back = .true。)第一次出现“chars”中没有包含的任何字符。 它返回一个给出该字符位置的整数,如果只找到“chars”中的字符,则返回零 8adjustl(string) 它左对齐“字符串”中包含的字符 9adjustr(string) 它正确地证明了“字符串”中包含的字符 10len_trim(string) 它返回一个等于“string”(len(string))长度的整数减去尾随空格的数量 11repeat(string,ncopy) 它返回一个长度等于“ncopy”的字符串乘以“string”的长度,并包含“ncopy”连接的“string”副本 例子1 (Example 1)此示例显示了index函数的使用 - program testingChars implicit none character (80) :: text integer :: i text = 'The intrinsic data type character stores characters and strings.' i=index(text,'character') if (i /= 0) then print *, ' The word character found at position ',i print *, ' in text: ', text end if end program testingChars编译并执行上述程序时,会产生以下结果 - The word character found at position 25 in text : The intrinsic data type character stores characters and strings. 例子2 (Example 2)此示例演示了trim功能的使用 - program hello implicit none character(len = 15) :: surname, firstname character(len = 6) :: title character(len = 25)::greetings title = 'Mr.' firstname = 'Rowan' surname = 'Atkinson' print *, 'Here is', title, firstname, surname print *, 'Here is', trim(title),' ',trim(firstname),' ', trim(surname) end program hello编译并执行上述程序时,会产生以下结果 - Here isMr. Rowan Atkinson Here isMr. Rowan Atkinson 例子3 (Example 3)这个例子演示了achar功能的使用 - program testingChars implicit none character:: ch integer:: i do i = 65, 90 ch = achar(i) print*, i, ' ', ch end do end program testingChars编译并执行上述程序时,会产生以下结果 - 65 A 66 B 67 C 68 D 69 E 70 F 71 G 72 H 73 I 74 J 75 K 76 L 77 M 78 N 79 O 80 P 81 Q 82 R 83 S 84 T 85 U 86 V 87 W 88 X 89 Y 90 Z 检查字符的词法顺序以下函数确定字符的词汇顺序 - Sr.No 功能说明 1lle(char, char) 比较第一个字符是否在词法上小于或等于第二个字符 2lge(char, char) 比较第一个字符是否在词法上大于或等于第二个字符 3lgt(char, char) 比较第一个字符是否在词法上大于第二个字符 4llt(char, char) 比较第一个字符是否在词法上小于第二个字符 Example 4 以下功能演示了使用方法 - program testingChars implicit none character:: a, b, c a = 'A' b = 'a' c = 'B' if(lgt(a,b)) then print *, 'A is lexically greater than a' else print *, 'a is lexically greater than A' end if if(lgt(a,c)) then print *, 'A is lexically greater than B' else print *, 'B is lexically greater than A' end if if(llt(a,b)) then print *, 'A is lexically less than a' end if if(llt(a,c)) then print *, 'A is lexically less than B' end if end program testingChars编译并执行上述程序时,会产生以下结果 - a is lexically greater than A B is lexically greater than A A is lexically less than a A is lexically less than B Fortran - 数字( Numbers) Fortran - Strings( Strings).下一篇> |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |